package com.example.treehole.adapter;


import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.drawable.GradientDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.treehole.R;
import com.example.treehole.utils.DynamicColorUtils;
import com.example.treehole.viewobject.DynamicOutSide;
import com.squareup.picasso.Picasso;

import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;


public class DynamicMyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context mContext;
    //  private OnItemClickListener mListener;
    private List<DynamicOutSide> data;

    public void setData(List<DynamicOutSide> data) {
        this.data = data;
    }

    public DynamicMyAdapter(Context mContext) {
        this.mContext = mContext;
    }

    public DynamicMyAdapter(Context context, List<DynamicOutSide> data) {
        this.mContext = context;
        //  this.mListener = listener;
        this.data = data;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_dynamic3, parent, false);
        return new ViewHolder(view);

    }

    @SuppressLint({"WrongConstant", "SetTextI18n"})
    @Override
    // 通过getItemViewType的返回值来选择具体的item显示
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, @SuppressLint("RecyclerView") final int position) {
        GradientDrawable drawable = new GradientDrawable();
        double score = data.get(position).getScore();
        //虽然本身cardView设置了圆角 但这里又设置背景颜色会将圆角顶掉
        drawable.setCornerRadius(20f);

        Map<String, Integer> colors = DynamicColorUtils.getItemColorsByScore(mContext, score);
        drawable.setColor(colors.get("bgColor"));

        ViewHolder vh = (ViewHolder) holder;
        vh.itemView.setBackground(drawable);
        //设置颜色
        vh.tvTitle.setTextColor(colors.get("titleColor"));
        vh.tvTime.setTextColor(colors.get("timeColor"));
        vh.tvContent.setTextColor(colors.get("contentColor"));
        //填充内容
        vh.tvContent.setText(data.get(position).getTvContent());
        if (data.get(position).getTvTitle()!=null && !data.get(position).getTvTitle().equals(""))
            vh.tvTitle.setText(data.get(position).getTvTitle());
        else
            vh.tvTitle.setVisibility(View.GONE);
        vh.tvTime.setText(data.get(position).getTvTime());
        vh.tvVisits.setText(data.get(position).getTvVisits()+"");
        vh.tvComments.setText(data.get(position).getTvComments()+"");
        vh.tvStarts.setText(data.get(position).getTvStars()+"");
        if (data.get(position).getIvContent()!=null && !data.get(position).getIvContent().equals(""))
            Picasso.get().load(data.get(position).getIvContent()).resize(400,400).centerCrop().into(vh.ivContent);
        else{
            vh.ivContent.setVisibility(View.GONE);
        }

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.onClick(position);
            }
        });
        holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                listener.onCLickLong(position);
                return true;
            }
        });

    }


    @Override
    public int getItemCount() {
        if (data == null) {
            return 0;
        } else {
            return data.size();
        }
    }


    static class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_time)
        TextView tvTime;
        @BindView(R.id.tv_title)
        TextView tvTitle;
        @BindView(R.id.tv_content)
        TextView tvContent;
        @BindView(R.id.iv_content)
        ImageView ivContent;
        @BindView(R.id.tv_visits)
        TextView tvVisits;
        @BindView(R.id.tv_comments)
        TextView tvComments;
        @BindView(R.id.tv_starts)
        TextView tvStarts;
        ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }


    //接口
    public interface OnItemClickListener {
        void onClick(int pos);
        void onCLickLong(int pos);
    }

    private OnItemClickListener listener;

    public void setOnMyItemClickListener(OnItemClickListener listener) {
        this.listener = listener;
    }

}


